package com.wowangz.cms.dailyoperation.storeuser.service;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.wowangz.cms.common.utils.SysConstant;
import com.wowangz.cms.dailyoperation.store.pojo.AdminUser;
import com.wowangz.cms.syscore.autotask.pojo.StoreLoginUser;
import com.wowangz.cms.syscore.exception.ServiceException;
import com.wowangz.cms.syscore.service.BaseService;
import com.wowangz.cms.syscore.utils.JedisUtil;
import com.wowangz.cms.systemmanage.city.pojo.City;
import com.wowangz.cms.systemmanage.country.service.CountryService;

/**
 * @author HUANGYUAN
 * @时间 2015年7月30日 上午10:57:29
 * @包名称 com.wowangz.cms.dailyoperation.storeuser.service
 * @描述 商户用户
 */
public class AdminUserService extends BaseService{

	private Logger logger = LoggerFactory.getLogger(CountryService.class);

	/**
	 * 新增省信息
	 * @param province
	 * @throws Exception
	 */
	public void add(AdminUser adminUser) throws ServiceException  {
		this.getSqlSession().insert("StoreUser.insertAdminUser", adminUser);
		//在redis中插入一条记录
		addUserToRedis(adminUser);
	}
	/**
	 * 分页查询总条数
	 * 
	 * @param map
	 * @return
	 */
	public long findResultCount(HashMap<String, Object> map) throws ServiceException {
		Long countNum = this.getSqlSession().selectOne("StoreUser.queryAdminUserCount", map);
		return countNum;
	}

	/**
	 * 查询记录集合
	 * 
	 * @param map
	 * @return
	 */
	public List<AdminUser> findResultList(HashMap<String, Object> map) throws ServiceException {
		List<AdminUser> list = this.getSqlSession().selectList("StoreUser.queryAdminUserPage", map);
		return list;
	}
	
	/**
	 * 验证信息
	 * @param map
	 * @return
	 * @throws Exception
	 */
	public boolean validationAdminUser(HashMap<String, Object> map) throws ServiceException {
		StoreLoginUser tmp = (StoreLoginUser)JedisUtil.getPublicDataObject((String)map.get("loginname"), StoreLoginUser.class, JedisUtil.STORE_LOGINUSER);
		if(tmp != null){
			return false;
		}else{
			return true;
		}
	}
	
	public AdminUser findAdminUserById(String id) throws ServiceException {
		AdminUser adminUser = this.getSqlSession().selectOne("StoreUser.findAdminUserById", id);
		return adminUser;
	}
	
	public void update(AdminUser adminUser) throws ServiceException {
		this.getSqlSession().update("StoreUser.updateAdminUser", adminUser);
		//将redis中的user更新
		adminUser = findAdminUserById(adminUser.getId().toString());
		addUserToRedis(adminUser);
	}
	
	public void delete(AdminUser adminUser) throws ServiceException {
		this.getSqlSession().update("StoreUser.updateAdminUser", adminUser);
		//从redis中删除登陆用户
		adminUser = findAdminUserById(adminUser.getId().toString());
		JedisUtil.deletePublicDataObject(adminUser.getLoginname(), JedisUtil.STORE_LOGINUSER);
	}
	
	public List<City> findCityByProvinceId(Long id) throws ServiceException {
		List<City> list = this.getSqlSession().selectList("City.findCityByProvinceId", id);
		return list;
	}
	
	public void addUserToRedis(AdminUser adminUser)
	{
		//在redis中插入一条记录
		StoreLoginUser storeLoginUser = new StoreLoginUser();
		storeLoginUser.setId(adminUser.getId());
		storeLoginUser.setStoreid(adminUser.getStoreid());
		storeLoginUser.setLoginname(adminUser.getLoginname());
		storeLoginUser.setPassword(adminUser.getPassword());
		storeLoginUser.setValidstart(adminUser.getValidstart());
		storeLoginUser.setValidend(adminUser.getValidend());
		storeLoginUser.setPwdchangedate(adminUser.getPwdchangedate());
		storeLoginUser.setCanLogin(SysConstant.YesOrNo.YES);
		storeLoginUser.setUsertype(SysConstant.StoreUserType.ADMIN);
		storeLoginUser.setUsername(adminUser.getLoginname());
		JedisUtil.insertPublicDataObject(storeLoginUser.getLoginname(), storeLoginUser , JedisUtil.STORE_LOGINUSER);
	}
	
	/**
	 * @author HUANGYUAN
	 * @时间 2015年7月30日 上午10:56:54
	 * @param paramMap
	 * @描述 修改用户的密码
	 */
	public void modifyStoreUserPassword(Map<String, Object> paramMap){
		try {
			this.getSqlSession().update("StoreUser.modifyStoreUserPassword", paramMap);
			AdminUser adminUser = findAdminUserById(paramMap.get("id").toString());
			addUserToRedis(adminUser);
		} catch (ServiceException e){
			e.printStackTrace();
			throw new ServiceException(e.getMessage());
		}
	}
}
